﻿using DLL.NET60.DB.MS.Entity.DTO;
using DLL.NET70.Infrastructure.EventAggregator;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.Standard.Infrastructure.Entity;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;

using System;
using System.Collections.Generic;
using System.Linq;

namespace PrismModule.MS.EditDrugs.ViewModels
{
    public class PUC_DialogEditDrugViewModel : CPDMVVMBase
    {
        private IRegionManager _regionManager;
        private IEventAggregator _eventAggregator;

        public PUC_DialogEditDrugViewModel(IRegionManager regionManager, IEventAggregator eventAggregator) : base(regionManager, eventAggregator)
        {
            _regionManager = regionManager;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<CCommandMessageEvent>().Subscribe(MessageReceived);//注册消息接收相应函数
        }

        private bool SaveOk = false;

        private void MessageReceived(CCommandMessage message)
        {
            Message = "保存数据完成";
        }

        public override void OnDialogClosed()
        {
            _eventAggregator.GetEvent<CCommandMessageEvent>().Unsubscribe(MessageReceived);//注销消息接收相应函数
            _regionManager.Regions.Remove("EditDrugContentRegion");
        }

        public override void OnDialogOpened(IDialogParameters parameters)
        {
            DTO_adi_dic_drug drug = parameters.GetValue<DTO_adi_dic_drug>("DrugCode");
            NavigateToEidtDrug(drug);
        }

        private void NavigateToEidtDrug(DTO_adi_dic_drug drug)
        {
            var parameters = new NavigationParameters();
            parameters.Add("adidrug", drug);
            if (drug != null)
            {
                string navigatePath = "PUC_EditDrug";
                _regionManager.RequestNavigate("EditDrugContentRegion", navigatePath, parameters);
            }
        }
    }
}